<template>
	<view class="shop-select-page">
		<!-- 地址选择栏 -->
		<view class="address-bar" @tap="xzMap">
			<view class="address-text">{{address || "请选择"}}</view>
			<image src="https://miaoyao-oss.oss-cn-hangzhou.aliyuncs.com/zqth/Right.png" mode="scaleToFill"></image>
		</view>

		<!-- 门店列表 -->
		<view class="shops-list">
			<view v-for="(shop, index) in shops" :key="index" class="shop-item" :class="{ 'selected': shop.isSelected }"
				@click="selectShop(index,shop)">
				<!-- 左侧：门店信息（名称、地址、营业时间、可预约标签） -->
				<view class="shop-info">
					<view class="shop-name">{{ shop.title }}</view>
					<view class="shop-address">
						<image src="https://miaoyao-oss.oss-cn-hangzhou.aliyuncs.com/zqth/Local.png" mode="scaleToFill">
						</image>
						{{ shop.address }}
					</view>
					<view class="shop-time">
						<image src="https://miaoyao-oss.oss-cn-hangzhou.aliyuncs.com/zqth/Time.png" mode="scaleToFill">
						</image>
						{{ shop.start_time }}-{{ shop.end_time }}
					</view>
					<view class="shop-reserve">可预约</view>
				</view>

				<!-- 右侧：距离 + 导航图标 -->
				<view class="shop-right">
					<view class="shop-distance">距离{{ shop.distance }}</view>
					<image src="https://miaoyao-oss.oss-cn-hangzhou.aliyuncs.com/zqth/Navigation.png"
						mode="scaleToFill"></image>
				</view>
			</view>
		</view>
	</view>
</template>
<script>
	import $api from "@/api/index.js"
	export default {
		data() {
			return {
				// 门店列表数据
				shops: [],
				page: 1,
				total: 0,
				address: ''
			};
		},
		onLoad() {
			this.getData()
		},
		onReachBottom() {
			if (this.shops.length >= this.total) return;
			this.page = this.page + 1;
			this.getData();
		},
		methods: {
			async getData() {
				let d = await $api.shopstore.storeList({
					page: this.page,
					coach_name: "",
					lat: 30.326608,
					lng: 120.175838
				})
				console.log(d);
				d.data.map(v => {
					v.isSelected = false
				})
				// d.data[0].isSelected = true
				this.shops = this.shops.concat(d.data)
			},
			xzMap() {
				// uni.getLocation({
				// 	success: (res) => {
				// 		console.log('当前位置的经度：' + res.longitude);
				// 		console.log('当前位置的纬度：' + res.latitude);


				// 	}
				// });
				uni.chooseLocation({
					latitude: 30.326608,
					longitude: 120.175838,
					success: (res1) => {
						// console.log('位置名称：' + res1.name);
						// console.log('详细地址：' + res1.address);
						// console.log('纬度：' + res1.latitude);
						// console.log('经度：' + res1.longitude);
						this.address = res1.name
						this.shops = []
						this.page = 1
						this.getData();
					}
				});

			},
			// 选中门店（点击时切换选中状态）
			selectShop(index, shop) {
				this.shops.forEach((shop, i) => {
					shop.isSelected = i === index;
				});
				uni.navigateBack({
					delta: 1
				});
				uni.$emit('update', shop)
			},
		},
	};
</script>
<style scoped lang="scss">
	.shop-select-page {
		background-color: #f5f5f5;
		height: 100vh;
		display: flex;
		flex-direction: column;

		// 地址选择栏
		.address-bar {
			display: flex;
			align-items: center;
			width: 750rpx;
			height: 112rpx;
			background-color: #fff;
			padding: 0 32rpx;
			box-sizing: border-box;

			.address-text {
				font-weight: bold;
				font-size: 28rpx;
				color: #222222;
				line-height: 36rpx;
				margin-right: 16rpx;
			}

			image {
				width: 28rpx;
				height: 28rpx;
			}
		}


		// 门店列表
		.shops-list {
			flex: 1;
			overflow-y: auto; // 列表滚动
			padding: 0 8px;
			margin-top: 16rpx;

			.shop-item {
				display: flex;
				justify-content: space-between;
				background-color: #fff;
				border-radius: 8px;
				padding: 40rpx;
				margin-bottom: 8px;
				position: relative; // 用于定位“选中角标”
				border: 1px solid transparent; // 初始透明边框，选中时显示绿色

				// 选中状态样式
				&.selected {
					// border-color: #4cd964; // 绿色边框
					background: url("https://miaoyao-oss.oss-cn-hangzhou.aliyuncs.com/zqth/itemback.png") no-repeat;
					background-size: 100% 100%;
				}

				// 左侧：门店信息区
				.shop-info {
					display: flex;
					flex-direction: column;
					justify-content: center;

					.shop-name {
						font-weight: 800;
						font-size: 32rpx;
						color: #222222;
						line-height: 36rpx;
						margin-bottom: 32rpx;
					}

					.shop-address,
					.shop-time {
						display: flex;
						align-items: center;
						font-weight: bold;
						font-size: 24rpx;
						color: #999999;
						margin-bottom: 8rpx;

						image {
							width: 28rpx;
							height: 28rpx;
							margin-right: 8rpx;
						}
					}

					.shop-reserve {
						width: 98rpx;
						height: 36rpx;
						border-radius: 8rpx 8rpx 8rpx 8rpx;
						border: 2rpx solid #D5D2D2;
						font-weight: bold;
						font-size: 20rpx;
						color: #D5D2D2;
						box-sizing: border-box;
						text-align: center;
						line-height: 36rpx;
						margin-top: 8rpx;
					}
				}

				// 右侧：距离 + 导航图标
				.shop-right {
					display: flex;
					flex-direction: column;
					align-items: flex-end;
					justify-content: center;

					.shop-distance {
						font-weight: bold;
						font-size: 24rpx;
						color: #999999;
						margin-bottom: 20rpx;
					}

					image {
						width: 64rpx;
						height: 64rpx;
					}
				}

			}
		}
	}
</style>